import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  HttpCode,
  HttpStatus,
  Query,
  Put,
  NotFoundException,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { TagsService } from './tags.service';
import {
  CreateTagDto,
  UpdateTagDto,
  QueryTagDto,
  TagResponseDto,
  TagsListResponseDto,
} from './dto';
import { UuidValidationPipe } from '../common/pipes/uuid-validation.pipe';

@ApiTags('Tag')
@Controller('tags')
export class TagsController {
  constructor(private readonly tagsService: TagsService) {}

  @Get()
  @ApiOperation({
    summary: '查询标签列表',
    description: '获取所有标签列表，支持按标签名称进行模糊查询',
  })
  @ApiResponse({ status: 200, description: '返回标签列表', type: TagsListResponseDto })
  findAll(@Query() queryDto: QueryTagDto): Promise<TagsListResponseDto> {
    return this.tagsService.findAll(queryDto);
  }

  @Get('name/:name')
  @ApiOperation({
    summary: '根据精确名称查询标签',
    description: '根据标签的精确名称查询单个标签，由于标签名称唯一，将返回单个标签或404错误',
  })
  @ApiParam({ name: 'name', description: '标签的精确名称', example: '工作效率' })
  @ApiResponse({ status: 200, description: '返回标签详情', type: TagResponseDto })
  @ApiResponse({ status: 404, description: '标签不存在' })
  async findByExactName(@Param('name') name: string): Promise<TagResponseDto> {
    try {
      return await this.tagsService.findByExactName(name);
    } catch (error) {
      if (error instanceof NotFoundException) {
        throw error;
      }
      throw new NotFoundException(`Tag with name "${name}" not found`);
    }
  }

  @Post()
  @ApiOperation({ summary: '创建标签', description: '创建一个新的标签' })
  @ApiResponse({ status: 201, description: '标签创建成功', type: TagResponseDto })
  @ApiResponse({ status: 409, description: '标签名称已存在' })
  create(@Body() createTagDto: CreateTagDto): Promise<TagResponseDto> {
    return this.tagsService.create(createTagDto);
  }

  @Get(':tagId')
  @ApiOperation({ summary: '获取标签详情' })
  @ApiParam({ name: 'tagId', description: '标签的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 200, description: '返回标签详情', type: TagResponseDto })
  @ApiResponse({ status: 404, description: '标签不存在' })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  findOne(@Param('tagId', UuidValidationPipe) id: string): Promise<TagResponseDto> {
    return this.tagsService.findOne(id);
  }

  @Delete(':tagId')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '删除标签' })
  @ApiParam({ name: 'tagId', description: '标签的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 204, description: '标签删除成功' })
  @ApiResponse({ status: 404, description: '标签不存在' })
  @ApiResponse({ status: 409, description: '标签正在被使用，无法删除' })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  remove(@Param('tagId', UuidValidationPipe) id: string): Promise<void> {
    return this.tagsService.remove(id);
  }

  @Put(':tagId')
  @ApiOperation({ summary: '更新标签', description: '更新标签的详细信息' })
  @ApiParam({ name: 'tagId', description: '标签的唯一标识', format: 'uuid' })
  @ApiResponse({ status: 200, description: '标签更新成功', type: TagResponseDto })
  @ApiResponse({ status: 404, description: '标签不存在' })
  @ApiResponse({ status: 409, description: '标签名称已存在' })
  @ApiResponse({ status: 400, description: '无效的UUID格式' })
  update(
    @Param('tagId', UuidValidationPipe) id: string,
    @Body() updateTagDto: UpdateTagDto,
  ): Promise<TagResponseDto> {
    return this.tagsService.update(id, updateTagDto);
  }
}
